package com.beaven.model.data.helper;

import com.beaven.model.model.api.RspModel;
import com.beaven.model.model.api.user.CategoryModel;
import com.beaven.model.model.card.CategoryCard;
import com.beaven.model.model.card.UserCard;
import com.beaven.model.model.db.Category;
import com.beaven.model.model.db.Category_Table;
import com.beaven.model.model.db.User;
import com.beaven.model.model.db.User_Table;
import com.beaven.model.net.HTTPService;
import com.beaven.model.net.NetFactory;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import java.io.IOException;
import java.util.List;
import retrofit2.Response;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * author: Beaven
 * time: 2017/5/26 11:43
 */

public class UserHelper {

  // 搜索用户
  public static Observable<RspModel<List<UserCard>>> searchByName(String name) {
    HTTPService service = NetFactory.getService();
    return service.searchUser(name)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }

  // 添加好友
  public static Observable<RspModel<UserCard>> addUser(String userId, String categoryId) {
    HTTPService service = NetFactory.getService();
    return service.addUser(userId, categoryId)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }

  // 获取好友列表
  public static Observable<RspModel<List<UserCard>>> getContacts() {
    HTTPService service = NetFactory.getService();
    return service.contract()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }

  // 获取分组列表
  public static Observable<RspModel<List<CategoryCard>>> getCategories() {
    HTTPService service = NetFactory.getService();
    return service.getCategories()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }

  public static Observable<RspModel<CategoryCard>> addCategory(CategoryModel categoryModel) {
    HTTPService service = NetFactory.getService();
    return service.createCategory(categoryModel)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }

  public static Observable<RspModel<UserCard>> deleteUser(String userId) {
    HTTPService service = NetFactory.getService();
    return service.deleteUser(userId)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }

  public static User findById(String id) {
    User user = findFromLocal(id);
    if (user == null) {
      user = findFromNet(id);
      if (user != null) {
        user.save();
      }
    }
    return user;
  }

  private static User findFromNet(String id) {
    HTTPService service = NetFactory.getService();
    try {
      Response<RspModel<UserCard>> response = service.getUser(id).execute();
      RspModel<UserCard> rspModel = response.body();
      if (rspModel != null) {
        return rspModel.getResult().build();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  // 从本地查询一个用户的信息
  public static User findFromLocal(String id) {
    return SQLite.select().from(User.class).where(User_Table.id.eq(id)).querySingle();
  }

  public static Category findCategory(String id) {
    return SQLite.select().from(Category.class).where(Category_Table.id.eq(id)).querySingle();
  }
}
